{% extends "base.html" %}

{% block css %}
<link rel="stylesheet" type="text/css" href="/statics/css/booking.css" />
{% endblock %}

{% block javascript %}
<script type="text/javascript" src="/statics/javascript/booking.js"></script>
<script type="text/javascript" src="/statics/javascript/jquery.blockUI.js"></script>
<script type="text/javascript">
</script>
{% endblock %}

{% block title %}今日订单{% endblock %}

{% block content %}
<h1>今日订单</h1>
{% if manCharge %}
<h2>温馨提示:如果你没交钱，麻烦你尽快去交钱,今天请把钱交给<a href="/user">{{manCharge}}</a></h2>
{% else %}
<h2><a href="/user">今天把钱交给谁呢?</a></h2>
{% endif %}
{% if order_datas %}
    {% for val in order_datas.values %}
    <table width="600" border="1" style="border-collapse: collapse">
    <tr>
        <th colspan="3">店铺：{{val.shop.name}}--电话：{{val.shop.telephone}}--本店金额：{{val.shop_price}}</th>
        <th width="100">实际收取</th>
        <th>欠费</th>
    </tr>
    {% for user_val in val.users.values %}
    <script>	    
    
    	function calShouldPayByUserCode(code){
    	    var sum = 0;
    		if(code == null){
    			alert('ERROR');
    			return;
    		}else{
    			$('.'+code).each(function(i){
    			     //alert($(this).text());
    			     sum = eval(sum + eval($(this).text()));
    			});
    		}
    		return sum;
    		
    	}
    	
	    $(function(){ 
           	    
	       $(jQueryId("treal_{{ user_val.user.code }}_{{val.shop.telephone}}")).blur(function(){
	          //计算欠费	          
	          var realPay = $(jQueryId("treal_{{ user_val.user.code }}_{{val.shop.telephone}}")).val();
	          if(realPay==undefined){
	               realPay = 0;
	          }
	          var shouldPay = calShouldPayByUserCode('{{ user_val.user.code }}');
              var balance = eval(shouldPay - realPay).toFixed(2);
              if(balance==undefined){
                balance = 0;
              }
              //更新数据
              $.post("/balance",{
                'key'       : '{{user_val.balance.balanceKey}}',
                'realPay' : realPay, 
                'balance'   : balance
	            },  function(data){
	                 var data = $.xml2json(data);
	                 if(data.success){
	                     showMessage(data.success);
	                 } else if(data.error) {
	                     showMessage(data.error);
	                 }
	             }, 'xml');
              
              //更新人为单位的实际付款              
              $(".treal_{{user_val.user.code}}").each(function(i){
                 $(this).val(realPay);
              });
              
              //更新结余
              $(".balance_{{ user_val.user.code}}").each(function(i){
                 $(this).text("");
                 $(this).text(balance);
              });
	       });	       
	        
	    });
    </script>
    <tr align="center">
        <td width="100">{{ user_val.user.name }}</td>
        <td width="350">{% for order in user_val.orders %}{{ order.dish.name }}{% if not forloop.last%}+{% endif %}{% endfor %}</td>
        <td id="shouldPay_{{ user_val.user.code }}" width="50" class="{{ user_val.user.code}}">{{ user_val.price }}</td>
        {% if user_val.balance %}
            <td width="50"><input type='text' class="treal_{{user_val.user.code}}" id='treal_{{ user_val.user.code }}_{{val.shop.telephone}}' value="{{user_val.balance.realPay}}"/></td>
            <td id="balance_{{ user_val.user.code }}" class="balance_{{ user_val.user.code}}" width="50">{{user_val.balance.balancePay}}</td>
        {% endif %}
        {% if not user_val.balance %}
            <td colspan="2"></td>
        {% endif %}
    </tr>
    {% endfor %}
    </table>
    {% endfor %}
{% endif %}
总金额: {{ total_price }}
{% endblock %}